package com.iot.comprehensive.face.controller;

import com.iot.comprehensive.face.dto.UserInfo;
import com.iot.comprehensive.face.model.Account;
import com.iot.comprehensive.face.model.User;
import com.iot.comprehensive.face.service.AccountService;
import com.iot.comprehensive.face.service.UserService;
import com.iot.comprehensive.face.utils.Jwt;
import com.iot.comprehensive.face.utils.PatternUtils;
import com.iot.comprehensive.face.utils.Result;
import com.iot.comprehensive.face.utils.ResultCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author 朱泽玉
 * @Create 2021/6/27 16:54
 * @Description
 * @Modified By:
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户操作", protocols = "http,https")
@ApiResponses({
        @ApiResponse(code = 200, message = "登录成功"),
        @ApiResponse(code = 300, message = "出现错误"),
        @ApiResponse(code = 301, message = "无权限(未认证)")
})
public class UserController {
    final UserService userService;
    final com.iot.comprehensive.face.utils.Jwt jwt;
    final AccountService accountService;
    public UserController(UserService userService, Jwt jwt, AccountService accountService) {
        this.userService = userService;
        this.jwt = jwt;
        this.accountService = accountService;
    }
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @ApiOperation(value = "获取个人信息",notes="获取个人信息")
    public Result info(HttpServletRequest request) throws IOException {
        // 1、获取返回的token
        Claims token = jwt.token(request);
        // 2、判断是否携带token
        if (token == null) {
            return new Result(ResultCode.FAIL, "请重新登录后再次尝试");
        }
        // 3、获取token中的id值
        Long id = Long.valueOf(token.getId());
        Account account = accountService.findById(id);
        // 4、获取个人信息
        User info = userService.getByAccount(id);
        return new Result(ResultCode.SUCCESS,info,account);
    }


    @RequestMapping(value = "" , method = RequestMethod.PUT)
    @ApiOperation(value = "修改个人信息",notes="修改个人信息")
    public Result update(HttpServletRequest request, @RequestBody UserInfo newInfo) throws IOException {
        // 1、获取返回的token
        Claims token = jwt.token(request);
        // 2、判断是否携带token
        if (token == null) {
            return new Result(ResultCode.FAIL, "请重新登录后再次尝试");
        }
        // 3、获取token中的id值
        Long id = Long.valueOf(token.getId());
        // 4、获取个人信息
        User info = userService.getByAccount(id);
        // 5、映射信息，讲newInfo映射到info上
        map(info,newInfo);
        userService.add(info);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户/进行用户注册", notes = "添加用户,根据手机号进行添加")
    public Result add(@RequestBody UserInfo certDto, HttpServletRequest request) {
        try {
            // 5、获取提交人的账号信息
            Account account = accountService.findByMobile(certDto.getMobile());
            // 6、用户信息提交
            User user = new User();
            map(user,certDto);
            user.setDate_time(PatternUtils.GetTime());
            user.setId(account.getId());
            user.setIdNumber(certDto.getIdNumber());
            userService.add(user);
            // 账号中修改为已认证
            account.setIsCertified(1);
            accountService.update(account);
            // 8、返回
            return new Result(ResultCode.SUCCESS);
        } catch (IOException e) {
            return new Result(ResultCode.FAIL, e);
        }
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除用户（未实现）", notes = "添加用户,根据手机号进行添加")
    public Result delete(@RequestBody UserInfo certDto, HttpServletRequest request) {
        return new Result(ResultCode.SUCCESS,"已删除");
    }



    void map(User user,UserInfo newUser){
        user.setAddress(newUser.getAddress());
        user.setAge(newUser.getAge());
        user.setDescription(newUser.getDescription());
        user.setSex(newUser.getSex());
        user.setLast_time(PatternUtils.GetTime());
        user.setName(newUser.getName());
    }
}
